package com.cskaoyan.wx.service;

import com.cskaoyan.bean.*;
import com.cskaoyan.mapper.KeywordMapper;
import com.cskaoyan.mapper.SearchHistoryMapper;
import com.cskaoyan.mapper.UserMapper;
import com.cskaoyan.utils.UserUtils;
import com.cskaoyan.wx.vo.SearchGoodsVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author LiaoLong
 * @date 2021-05-12 23:16
 */
@Service
public class SearchServiceImpl implements SearchService {
    @Autowired
    private KeywordMapper keywordMapper;

    @Autowired
    private SearchHistoryMapper searchHistoryMapper;

    @Autowired
    private UserMapper userMapper;

    @Override
    public SearchGoodsVO getSearchs(String username) {
        SearchGoodsVO searchGoodsVO = new SearchGoodsVO();
        KeywordExample keywordExample = new KeywordExample();
        KeywordExample.Criteria criteria = keywordExample.createCriteria();
        criteria.andIsDefaultEqualTo(true);
        criteria.andDeletedNotEqualTo(true);

        // 默认关键词
        List<Keyword> defaultKeywords = keywordMapper.selectByExample(keywordExample);
        assert defaultKeywords.size() <= 1;
        Keyword defaultKeyword = defaultKeywords.get(0);
        searchGoodsVO.setDefaultKeyword(defaultKeyword);

        // 热门关键词
        KeywordExample keywordExample1 = new KeywordExample();
        keywordExample.createCriteria().andDeletedNotEqualTo(true).andIsHotEqualTo(true);
        keywordExample.clear();
        List<Keyword> hotKeywordList = keywordMapper.selectByExample(keywordExample);
        searchGoodsVO.setHotKeywordList(hotKeywordList);

        Integer userId = UserUtils.getUserId();
        if (userId != null){
            SearchHistoryExample searchHistoryExample = new SearchHistoryExample();
            searchHistoryExample.createCriteria().andUserIdEqualTo(userId).andDeletedNotEqualTo(true).andDeletedNotEqualTo(true);
            List<SearchHistory> historyKeywordList = searchHistoryMapper.selectByExample(searchHistoryExample);
            searchGoodsVO.setHistoryKeywordList(historyKeywordList);
        }else {
            searchGoodsVO.setHistoryKeywordList(null);
        }

        return searchGoodsVO;

    }

    @Override
    public List<String> fuzzySearchKeywords(String keyword) {
        KeywordExample keywordExample = new KeywordExample();
        keywordExample.createCriteria().andKeywordLike("%" + keyword + "%").andDeletedNotEqualTo(true);
        List<Keyword> keywords = keywordMapper.selectByExample(keywordExample);
        ArrayList<String> keywordVOList = new ArrayList<>();
        for (Keyword keyword1 : keywords) {
            keywordVOList.add(keyword1.getKeyword());
        }
        return keywordVOList;
    }

    /*
       逻辑删除搜索历史
     */
    @Transactional
    @Override
    public void deleteSearchHistory(String username) {
        Integer userId = userMapper.selectUserIdByName(username);
        SearchHistoryExample searchHistoryExample = new SearchHistoryExample();
        searchHistoryExample.createCriteria().andUserIdEqualTo(userId);
        SearchHistory searchHistory = new SearchHistory();
        searchHistory.setDeleted(true);
        searchHistoryMapper.updateByExampleSelective(searchHistory, searchHistoryExample);
    }
}
